/* 
 * File:   TriangleList.cpp
 * Author: brady
 * 
 * Created on February 15, 2011, 11:54 AM
 */

#include "Mesh.h"

Mesh::Mesh() {
}

Mesh::~Mesh() {
}
inline
bool Mesh::TestIntersect(Ray &ray, double tmin, double tmax, HitStruct& hitStruct)
{
    return BVHRoot->TestIntersect(ray, tmin, tmax, hitStruct);
}
//const Vector3D Mesh::SurfaceNormal(Ray &ray)
//{
//    return hitShape->SurfaceNormal(ray);
//}

void Mesh::AddTriangle(Triangle* t)
{
    shapes.push_back(t);
}

void Mesh::AddTriangle(double x1, double y1, double z1,
                               double x2, double y2, double z2,
                               double x3, double y3, double z3)
{
    Triangle* t = new Triangle(Vector3D(x1, y1, z1),
                          Vector3D(x2, y2, z2),
                          Vector3D(x3, y3, z3));
    shapes.push_back(t);
}

void Mesh::AddTriangle(Vector3D p1, Vector3D p2, Vector3D p3)
{
    Triangle* t = new Triangle(p1, p2, p3);
    shapes.push_back(t);
}

void Mesh::SetShader(Shader* shader)
{
    this->shader = shader;
    for (int i = 0; i<shapes.size(); i++)
    {
        shapes[i]->SetShader(shader);
    }
}
inline
Shader* Mesh::GetShader()
{
    return shader;
}
inline
double Mesh::GetSortValue(int sortBy)
{
    if(sortBy == X_VAL){
        return (GetBoundingBox()->GetXMax() + GetBoundingBox()->GetXMin())/2.0;
    }
    if(sortBy == Y_VAL){
        return (GetBoundingBox()->GetYMax() + GetBoundingBox()->GetYMin())/2.0;
    }
    if(sortBy == Z_VAL){
        return (GetBoundingBox()->GetZMax() + GetBoundingBox()->GetZMin())/2.0;
    }
}

void Mesh::BuildBVH()
{
    BVHRoot = new BoundingVolumeNode(shapes, X_VAL);
    boundingBox = BVHRoot->GetBoundingBox();
}
